package com.uphone.edwards;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.radish.baselibrary.base.BaseApplication;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
//import com.uphone.edwards.dragger.component.DaggerMyAppComponent;
import com.uphone.edwards.dragger.component.DaggerMyAppComponent;
import com.uphone.edwards.dragger.component.MyAppComponent;
import com.uphone.edwards.dragger.module.MyAppModule;
import com.uphone.edwards.http.HttpManager;
import com.uphone.edwards.util.LanguageType;
import com.uphone.edwards.util.LanguageUtils;
import com.uphone.edwards.util.SharedUtils;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/05/24
 * desc   : xxxx 描述
 */
public class MyApp extends BaseApplication {
    private HttpManager mHttpManager = null;

    public static String languageType = "2";

    public static Context mContext;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void init() {
        mContext = this;
        //初始化网络
        mHttpManager = new HttpManager();
        /**
         * 对于7.0以下，需要在Application创建的时候进行语言切换
         */
        if ("".equals(SharedUtils.getShareLanguage(this))){
            SharedUtils.putShareLanguage("en" , this);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                LanguageUtils.changeAppLanguage(this, LanguageType.ENGLISH.getLanguage());
            }
        }else{
            String language = SharedUtils.getShareLanguage(this);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                LanguageUtils.changeAppLanguage(this, language);
            }
        }

        if ("en".equals(SharedUtils.getShareLanguage(this))){
            languageType = "2";
        }
        if ("ch".equals(SharedUtils.getShareLanguage(this))){
            languageType = "1";
        }
        if ("ja".equals(SharedUtils.getShareLanguage(this))){
            languageType = "3";
        }
        if ("ko".equals(SharedUtils.getShareLanguage(this))){
            languageType = "4";
        }

//        initBugly();


    }

    public static Context getmContext(){
        return mContext;
    }


    private void initBugly() {
        Bugly.init(getApplicationContext(), "a7c1082f9f", true);
        Beta.autoDownloadOnWifi = true;
        Beta.autoInit = true;
        Beta.autoCheckUpgrade = true;
        Beta.enableHotfix = true;
    }
    /**
     * 关联apiService
     *
     * @param clz
     * @param <T>
     * @return
     */
    public static <T> T apiService(Class<T> clz) {
        return (getInstance()).mHttpManager.getService(clz);
    }

    public static synchronized MyApp getInstance() {
        return (MyApp) mInstance;
    }


    public static MyAppComponent getAppComponent() {
        return DaggerMyAppComponent.builder()
                .myAppModule(new MyAppModule(getInstance()))
                .build();
    }


}
